<template>
    <section>
        <el-form :model="itemForm" :rules="rules" ref="itemFormRef" label-width="100px">
            <el-input type="hidden" v-model="itemForm.id" auto-complete="off"></el-input>
            <el-form-item label="序号:" prop="item_order_number">
                <el-input placeholder="请输入检查项序号" v-model="itemForm.item_order_number" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="检查项目:" prop="item_name">
                <el-input placeholder="请输入检查项目" type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="itemForm.item_name" ></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: center">
            <el-button @click="closeDialog('itemFormRef')">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate('itemFormRef')">保 存</el-button>
        </div>
    </section>
</template>

<script>
    import dict from '../../../common/js/dict';
    import {isNullTrue} from "../../../common/js/commons";

    export default {
        props: ['itemForm'],
        data() {
            var orderOneValid = (rule,value,callback) => {
                if(!isNullTrue(value)) {
                    var reg = /^[1-9]+[0-9]*]*$/;
                    if (!reg.test(value)) {
                        callback(new Error('只能输入数字!'));
                    } else {
                        callback();
                    }
                }
                callback();

            };
            return {
                dict: dict,
                rules: {
                    item_order_number: [
                        { required: true, message: '请输入检查项序号', trigger: 'blur' },
                        { validator: orderOneValid,trigger: ['change','blur']}
                    ],
                    item_name:[{ required: true, message: '请输入检查项目', trigger: 'blur' },
                        { min: 1, max: 200, message: '长度不超过200个字符', trigger: ['change','blur'] }
                    ]
                }
            }
        },
        methods: {

            addOrUpdate(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let condition = Object.assign(this.itemForm);
                        this.$emit("saveOneItem", condition);
                    }
                })
            },
            closeDialog(formName) {
                this.$emit('closeDialog');
                this.itemForm.item_name = '';
                this.$refs[formName].clearValidate();
                // this.$refs[formName].resetFields();
            }

        },
        watch: {
            itemForm (newVal, oldVal){
                this.itemForm = newVal;
            }
        },
        mounted(){ }
    }
</script>
<style scoped>

</style>
